package com.effectiv.gooruda.rest.api.controller;

import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.gooruda.rest.api.domain.CloudMessages;

import com.effectiv.gooruda.rest.api.operation.MessageOperations;

@Controller
@RequestMapping("/api/cq")
@Slf4j
public class MessageController implements MessageOperations {

	@Autowired
	@Qualifier("messageServiceImpl")
	private MessageOperations operations;
	
	
	//UC : 006
	@RequestMapping(value="/{queueName}/messages", method = RequestMethod.POST)
	@Override
	public @ResponseBody CloudMessages addMessages(@PathVariable("queueName") String queueName, @Valid @RequestBody CloudMessages messages) {
		
		log.info("Adding messages on queue = {}", queueName);
		
		operations.addMessages(queueName, messages);
		
		return messages;
	}
	
	//UC : 007
	@RequestMapping(value="/{queueName}/messages", method = RequestMethod.GET)
	@Override
	public @ResponseBody CloudMessages findAll(@PathVariable("queueName") String queueName, @RequestParam(value="max", required=false, defaultValue="1") int max, 
			@RequestParam(value="delete", required=false, defaultValue="false") boolean delete) {
		return operations.findAll(queueName, max, delete);
		
	}
	
	//UC : 008
	@RequestMapping(value="/{queueName}/messages/{messageId}", method = RequestMethod.DELETE)
	@Override
	public @ResponseBody boolean delete(@PathVariable("queueName") String queueName, @PathVariable("messageId") String messageId) {
		return operations.delete(queueName, messageId);
		
	}

	
	

}
